﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using School.Entities;

namespace School.Pages.Accounting
{
    public partial class Expenses_Screens : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlBind(ddlExp, School.Accounting.Expense.GetAllExpenses().ToList());
                ddlBind(ddlScreen, School.Accounting.Expense.GetALl_Screen().ToList());

                ddlBind(ddlExpSe, School.Accounting.Expense.GetALl_Expense().ToList());
                ddlBind(ddlScreenSe, School.Accounting.Expense.GetALl_Screen().ToList());
                gvEx_SecBind();
            }
            lbltestStation.Visible = false;
            lbltestStationSearch.Visible = false;
        }
        private void ddlBind(DropDownList ddl, object o)
        {
            ddl.DataSource = o;
            ddl.Items.Clear();
            ddl.Items.Insert(0, new ListItem("اختيار", "-1"));
            ddl.DataBind();
        }

        protected void btnSaveStation_Click(object sender, EventArgs e)
        {
            bool re = School.Accounting.Expense.AddToExpenses_Screens(Convert.ToInt32(ddlExp.SelectedValue), Convert.ToInt32(ddlScreen.SelectedValue.ToString()));
            if (re == true)
            {
                lbltestStation.Text = "تمت الاضافة بنجاح";
                ddlBind(ddlExp, School.Accounting.Expense.GetALl_Expense().ToList());
                ddlBind(ddlScreen, School.Accounting.Expense.GetALl_Screen().ToList());
                gvEx_SecBind();

            }
            else
            {
                lbltestStation.Text = "لم تتم عملية الاضافة.. ربما يكون مضاف من قبل .حاول مرة اخري";
            }
            lbltestStation.Visible = true;
        }

        protected void btnNew_Click1(object sender, EventArgs e)
        {
            plsearch.Visible = false;
            Paneladd.Visible = true;
        }

        protected void btnexit_Click1(object sender, EventArgs e)
        {

            plsearch.Visible = true;
            Paneladd.Visible = false;
        }

        protected void btnsearch_Click(object sender, EventArgs e)
        {
            gvEx_SecBind();
        }

        private void gvEx_SecBind()
        {
            int count;
            IEnumerable<ACC_Expenses_Screens> query = School.Accounting.Expense.Get_Expenses_Screens();
            if (ddlExpSe.SelectedIndex > 0)
            {
                int Ex_Id = Convert.ToInt32(ddlExpSe.SelectedValue);
                query = query.Where(s => s.Expense_Id == Ex_Id);

            }
            if (ddlScreenSe.SelectedIndex > 0)
            {
                int Se_Id = Convert.ToInt32(ddlScreenSe.SelectedValue);
                query = query.Where(s => s.Screen_Id == Se_Id);
            }

            var que = from q in query
                      select new
                          {
                              Screen_Name = q.SEC_Screen.Name,
                              Expense_Name = q.ACC_Expense.Name,
                              q.Id
                          };

            gvEx_Sec.DataSource = que;
            gvEx_Sec.DataBind();
            count = gvEx_Sec.Rows.Count;
            lblRecordNo.Text = "عدد نتائج البحث = " + count.ToString();
        }

        protected void gvEx_Sec_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvEx_Sec.PageIndex = e.NewPageIndex;
            gvEx_SecBind();

        }

        protected void gvEx_Sec_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int Id = Convert.ToInt32(gvEx_Sec.DataKeys[e.RowIndex].Value.ToString());
            bool re = School.Accounting.Expense.Delete_Expenses_Screens(Id);
            if (re == true)
            {
                lbltestStationSearch.Text = "تم الحذف بنجاح";
                gvEx_SecBind();
            }
            else
            {
                lbltestStationSearch.Text = "لم تتم عملية الحذف..حاول مرة اخري";
            }
            lbltestStationSearch.Visible = true;
        }
    }
}